package android.pot.com.smartcar.tool;

import android.os.Looper;
import android.pot.com.smartcar.R;
import android.pot.com.smartcar.application.MyApplication;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

/**
 * Created by zhaoshilu on 2017/8/25.
 */

public class TipToast {

    private  static Toast toast;

    public static void show(String tip){
        try {
            if(toast == null){
                toast = new Toast(MyApplication.getContext());
            }
            toast.setDuration(Toast.LENGTH_SHORT);
            toast.setGravity(Gravity.CENTER, 0, -100);
            View toastLayout = LayoutInflater.from(MyApplication.getContext()).inflate(R.layout.popwindow_tip, null);
            TextView txtToast = (TextView)toastLayout.findViewById(R.id.tv_title);
            txtToast.setText(tip);
            toast.setView(toastLayout);
            toast.show();
        }catch (Exception e){
            //解决在子线程中调用Toast的异常情况处理
            Looper.prepare();
            if(toast == null){
                toast = new Toast(MyApplication.getContext());
            }
            toast.setDuration(Toast.LENGTH_SHORT);
            toast.setGravity(Gravity.CENTER, 0, -100);
            View toastLayout = LayoutInflater.from(MyApplication.getContext()).inflate(R.layout.popwindow_tip, null);
            TextView txtToast = (TextView)toastLayout.findViewById(R.id.tv_title);
            txtToast.setText(tip);
            toast.setView(toastLayout);
            toast.show();
            Looper.loop();
        }

    }

    public static void show(String tip,int time){
        try {
            if(toast == null){
                toast = new Toast(MyApplication.getContext());
            }
            toast.setDuration(Toast.LENGTH_LONG);
            toast.setGravity(Gravity.CENTER, 0, 0);
            View toastLayout = LayoutInflater.from(MyApplication.getContext()).inflate(R.layout.popwindow_tip, null);
            TextView txtToast = (TextView)toastLayout.findViewById(R.id.tv_title);
            txtToast.setText(tip);
            toast.setView(toastLayout);
            toast.show();
        }catch (Exception e){
            //解决在子线程中调用Toast的异常情况处理
            Looper.prepare();
            if(toast == null){
                toast = new Toast(MyApplication.getContext());
            }
            toast.setDuration(Toast.LENGTH_LONG);
            toast.setGravity(Gravity.CENTER, 0, 0);
            View toastLayout = LayoutInflater.from(MyApplication.getContext()).inflate(R.layout.popwindow_tip, null);
            TextView txtToast = (TextView)toastLayout.findViewById(R.id.tv_title);
            txtToast.setText(tip);
            toast.setView(toastLayout);
            toast.show();
            Looper.loop();
        }

    }
}
